﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace CalendarMod.Views;
/// <summary>
/// Interaction logic for CalendarMain
/// </summary>
public partial class CalendarMain : UserControl
{
    public string[] titleWeek = new string[]
    {
        "星期日","星期一","星期二","星期三","星期四","星期五","星期六"
    };

    public CalendarMain()
    {
        InitializeComponent();
        this.Loaded += CalendarMain_Loaded;
        //CalendarListBox.Items.Add(new object());

    }

    private void CalendarMain_Loaded(object sender, RoutedEventArgs e)
    {
        GetMonthDays(2024, 9);

    }

    private UniformGrid GetCalendarUniformGrid(DependencyObject uniformGrid)
    {
        UniformGrid tempGrid = uniformGrid as UniformGrid;

        if (tempGrid != null)
        {
            return tempGrid;
        }

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(uniformGrid); i++)
        {
            DependencyObject gridReturn =
                GetCalendarUniformGrid(VisualTreeHelper.GetChild(uniformGrid, i));
            if (gridReturn != null)
            {
                return gridReturn as UniformGrid;
            }
        }
        return null;
    }
    private ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar();

    private int GetMonthDays(int year, int month)
    {
        var calendarDisplayUniformGrid = GetCalendarUniformGrid(CalendarListBox);

        int dayNum = DateTime.DaysInMonth(year, month);
        DateTime dateTime = new DateTime(year, month, 1);

        //日：0，一：1，二：2，三：3，四：4，五：5，六：6
        int firstDayIndex = (int)(dateTime.DayOfWeek);
        if (firstDayIndex + dayNum > 35)
        {
            calendarDisplayUniformGrid.Rows = 6 + 1;
        }
        else if (firstDayIndex + dayNum > 28)
        {
            calendarDisplayUniformGrid.Rows = 5 + 1;
        }
        else
        {
            calendarDisplayUniformGrid.Rows = 4 + 1;
        }

        for (int i = 0; i < titleWeek.Length; i++)
        {
            var title = titleWeek[i];
            Button dayButton = new Button();
            dayButton.HorizontalAlignment = HorizontalAlignment.Center;
            dayButton.VerticalAlignment = VerticalAlignment.Center;
            dayButton.Content = $"{title}";
            dayButton.BorderBrush = Brushes.Red;
            dayButton.BorderThickness = new Thickness(2);
            dayButton.Width = 72;
            dayButton.Height = 70;
            dayButton.FontSize = 12;
            CalendarListBox.Items.Add(dayButton);

        }

        for (int i = 1; i <= dayNum; i++)
        {
            var datetime = new DateTime(year, month, i,0,0,1);

            int lmonth = cCalendar.GetMonth(datetime);
            int lday = cCalendar.GetDayOfMonth(datetime);

            var dayChinese = GetChineseDay(lday);

            if (dayChinese == "初一")
            {
                dayChinese = GetChineseMonth(datetime);
            }

            Label hiddenLabel = new Label();
            hiddenLabel.HorizontalAlignment = HorizontalAlignment.Center;
            hiddenLabel.VerticalAlignment = VerticalAlignment.Center;
            hiddenLabel.Visibility = Visibility.Collapsed;
            hiddenLabel.Content = $"{i}";

            Label subsidiaryDateLabel = null;
            subsidiaryDateLabel = new Label();
            subsidiaryDateLabel.HorizontalAlignment = HorizontalAlignment.Center;
            subsidiaryDateLabel.VerticalAlignment = VerticalAlignment.Center;
            subsidiaryDateLabel.Content = dayChinese;


            StackPanel stackPanel = new StackPanel();
            stackPanel.Orientation = Orientation.Vertical;
            stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
            stackPanel.VerticalAlignment = VerticalAlignment.Center;

            stackPanel.Children.Add(hiddenLabel);
            stackPanel.Children.Add(subsidiaryDateLabel);


            Button dayButton = new Button();
            dayButton.HorizontalAlignment = HorizontalAlignment.Center;
            dayButton.VerticalAlignment = VerticalAlignment.Center;

            dayButton.Content = stackPanel;

            dayButton.BorderBrush = Brushes.Red;
            dayButton.BorderThickness = new Thickness(2);
            dayButton.Width = 72;
            dayButton.Height = 70;
            CalendarListBox.Items.Add(dayButton);
        }

        return 0;
    }

    /// <summary>
    /// 获取农历日期
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public  string GetChineseDay(int iDay)
    {
        string szText1 = "初十廿三";
        string szText2 = "一二三四五六七八九十";
        string strDay;
        if (iDay == 20)
        {
            strDay = "二十";
        }
        else if (iDay == 30)
        {
            strDay = "三十";
        }
        else
        {
            strDay = szText1.Substring((iDay - 1) / 10, 1);
            strDay = strDay + szText2.Substring((iDay - 1) % 10, 1);
        }
        return strDay;
    }
    public string GetChineseMonth(DateTime datetime)
    {
        int lmonth = cCalendar.GetMonth(datetime);

        if (lmonth < 13 && lmonth > 0)
        {
            return months[lmonth - 1] + "月";
        }

        throw new ArgumentOutOfRangeException("无效的月份!");
    }
    #region 农历月

    /// <summary>
    /// 农历月
    /// </summary>
    private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };

    #endregion
}
